﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using IndyMosaic.Models.Podcast;
using IndyMosaic.Core.Data;
using System.Text;

namespace IndyMosaic.Org.Site.Controllers
{
    public class PodcastController : Controller
    {
        IPodcastRepository repository { get; set; }

        #region Constructors
        public PodcastController() : this(new PodcastRepository()) { }
        public PodcastController(IPodcastRepository repository)
        {
            this.repository = repository;
        }
        #endregion

        //
        // GET: /Podcast/

        public ActionResult Index(int? page)
        {
            if (page != null && page > 0)
            {
                page--;
            }
            const int pageSize = 5;
            var podcastEntries = repository.ListAllEntries();
            var paginatedPodcastEntries = new PaginatedList<Entry>(podcastEntries, (page ?? 0), pageSize);

            return View(paginatedPodcastEntries);
        }

        //
        // GET: /Podcast/Details/5

        public ActionResult Details(int id)
        {
            var entry = repository.GetEntry(id);
            if (entry == null)
            {
                return View("NotFound");
            }
            else
            {
                return View(entry);
            }
        }

        //
        // GET: /Podcast/Create
        [Authorize(Roles = "Administrator")]
        public ActionResult Create()
        {
            Entry entry = new Entry();
            return View(entry);
        } 

        //
        // POST: /Podcast/Create

        [HttpPost]
        [Authorize(Roles = "Administrator")]
        public ActionResult Create(Entry entry)
        {
            try
            {
                repository.Add(entry);
                repository.Save();

                return RedirectToAction("Podcasts", "Administration");
            }
            
            catch
            {
                ModelState.AddRuleViolations(entry.GetRuleViolations());
                return View(entry);
            }
        }
        
        //
        // GET: /Podcast/Edit/5
 
        [Authorize(Roles="Administrator")]
        public ActionResult Edit(int id)
        {
            return View(repository.GetEntry(id));
        }

        //
        // POST: /Podcast/Edit/5

        [HttpPost]
        [Authorize(Roles = "Administrator")]
        public ActionResult Edit(int id, Entry newEntry)
        {
            try
            {
                Entry entry = repository.GetEntry(id);

                entry.DatePublished = newEntry.DatePublished;
                entry.Description = newEntry.Description;
                entry.FileURL = newEntry.FileURL;
                entry.Title = newEntry.Title;

                repository.Save();
 
                return RedirectToAction("Index");
            }
            catch
            {
                return View();
            }
        }


        #region Syndication
        public ActionResult RSS()
        {
            var entries = repository.ListLatestTopNEntries(10);
            StringBuilder sb = new StringBuilder(Request.Url.ToString());
            sb.Replace("RSS", "Details/{0}");
            
            return Content(repository.GetFeed(entries, sb.ToString(), Url),"application/rss+xml", Encoding.Unicode);
        }
        #endregion
    }
}
